package net.yxsoft.controller.pc.sysmgt.common;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import net.yxsoft.enumclass.SexEnumCls;
import net.yxsoft.kit.Helper;
import net.yxsoft.kit.ImageUploadKit;
import net.yxsoft.model.infrastructure.TsCode;
import net.yxsoft.model.infrastructure.TsParty;
import net.yxsoft.model.infrastructure.TsUdmkey;
import net.yxsoft.resource.Res;
import net.yxsoft.service.AreaService;
import net.yxsoft.service.infrastructure.CodeService;
import net.yxsoft.service.infrastructure.GroupService;
import net.yxsoft.service.infrastructure.MenuService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公共查询控制器
 */
public class ApiController extends AuthController {


  /**
  * @MethodName: loadResource
  * @Description: 在脚本中请求该方法获取项目资源配置
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  @Before(POST.class)
  public void loadResource() {
    Map<String, Object> rs = Helper.setUserResource(new HashMap<String, Object>(), getLoginUser());
    rs.put("PROJECT_NAME", Res.PAGE_TITLE_SYS);
    rs.put("DEV_MODE", Res.DEV_MODE);
    rs.put("sex", SexEnumCls.COLLECTS_DEFAULTS);

    // --------------------------- 开始定义 ---------------------------
    Record ad = Db.findFirst("select * from  ts_user u \n" +
                               "left join ts_ur_relation t on u.id=t.user_id\n" +
                               "left join ts_role r on r.id=t.role_id\n" +
                               "where u.id=? and r.code=?", getLoginUser().getId(), Res.ADMIN_ROLE_CODE);
    List<Record> codeList =Db.find("select codename,customid from ts_code where codetype='EDUCATION' and leaf='2'");
    rs.put("RECORD_LIST",codeList);
    if(null != ad) {
      rs.put("IS_ADMIN", 1);
    } else {
      rs.put("IS_ADMIN", 0);
    }

    List<Record> jobList = Db.find("select * from ts_code where codetype = 'POSITION' and LEAF = '2'");
    rs.put("JOB_LIST",jobList);


    List<Record> sTaskList=Db.find("select * from ts_code where parentid = '003026' and leaf = '2' and rootcustomid = 'BUSINESS'");
    rs.put("STASK_LIST",sTaskList);

    List<Record> fnFormList = Db.find("select id,name from fn_form where type=0");
    rs.put("FORM_LIST",fnFormList);

    List<Record> FileTypeList = Db.find("select * from ts_code where codetype = 'FJLX' and LEAF = '2'");
    rs.put("FILE_TYPE_LIST",FileTypeList);
    // --------------------------- 结束定义 ---------------------------

    renderJson(rs);
  }

  /**
  * @MethodName: captcha
  * @Description: 验证码
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  @Clear
  @ActionKey("/captcha")
  public void captcha() {
    renderCaptcha();
  }


  /**
  * @MethodName: getCodeByCustomid
  * @Description: 根据customid 获取单条数据
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getCodeByCustomid(){
    String customid = getPara("customid");
    String codeType=getPara("codeType");
    renderJson(CodeService.INSTANCE.getCodeByCustomid(customid,codeType));
  }

  /**
  * @MethodName: getCodeListByParentCustomId
  * @Description: parentCustomid 获取集合
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getCodeListByParentCustomId() {
    String leaf = getPara("leaf");
    String codeType=getPara("codeType");
    renderJson(CodeService.INSTANCE.getCodesByParentCustomId(leaf,codeType));
  }

  /**
  * @MethodName: getCodeListByParentCustomIdIncludeChoosName
  * @Description: parentCustomid 获取集合(可自定义传值)
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getCodeListByParentCustomIdIncludeChoosName(){
    String _leaf = getPara("leaf");
    String _chooseName = getPara("chooseName");
    String _codeType=getPara("codeType");
    List<TsCode> _models = CodeService.INSTANCE.getCodesByParentCustomId(_leaf,_codeType);
    if(StrKit.notBlank(_chooseName)) {
      TsCode _choose = new TsCode();
      _choose.setCustomid("");
      _choose.setCodename(_chooseName);
      _models.add(0, _choose);
    }
    renderJson(_models);
  }


  /**
  * @MethodName: getPartyNameList
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getPartyNameList() {
    List<TsParty> _models = TsParty.dao.find("SELECT NAME, NAME || '|'|| CNNAME NAMEGROUP FROM TS_PARTY ORDER BY NAME");
    renderJson(_models);
  }
  /**
  * @MethodName: getDataModelUDMKeyListByParty
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getDataModelUDMKeyListByParty() {
    String _partyName = getPara("partyName");
    if(_partyName == null || _partyName.equals("")) {
      renderJson(-99);
    }
    List<TsUdmkey> _models = TsUdmkey.dao.find("SELECT ATTRIB_20 value ,ATTRIB_02 text  FROM TS_UDMKEY WHERE ATTRIB_01 = ? ORDER BY conflict_id", _partyName);
    renderJson(_models);
  }
  /**
  * @MethodName: getCodeRulrList
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getCodeRulrList() {
    List<TsCode> _models = TsCode.dao.find("SELECT * FROM TS_CODE WHERE PARENTNAME = ? AND ENABLED = 1 ORDER BY SORTNO ASC,CODEID ASC", "更新规则");
    renderJson(_models);
  }

  /**
  * @MethodName: getUserRoleMenuList
  * @Description: 获取登录用户使用的当前功能模块的按钮权限
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getUserRoleMenuList() {
    renderJson(MenuService.INSTANCE.getUserRoleMenuList(getPara("menuCode"), getLoginUser().getId()));
  }

  /**
  * @MethodName: getUserRoleMenuListByFunction
  * @Description: 后台方法权限验证
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void getUserRoleMenuListByFunction() {
    renderJson(MenuService.INSTANCE.getUserRoleMenuListByFunction(getPara("functionCode"), getLoginUser().getId()));
  }

  /**
  * @MethodName: upload
  * @Description: 上传
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:34
  * @author jhf
  */
  public void upload() {

    UploadFile uploadFile = null;
    try {
      uploadFile = getFile();

    } catch(Exception ex) {

      renderJson(Ret.create("errno",1).set("data","上传出错"));
      return;
    }

    if(uploadFile == null) {
      renderJson(Ret.create("errno",1).set("data","上传出错"));
      return;
    }

    renderJson(ImageUploadKit.uploadContent("content", uploadFile.getFile()));

  }
  /**
  * @MethodName: findGroups
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:33
  * @author jhf
  */
  @Before(POST.class)
  public void findGroups() {
    renderJson(GroupService.INSTANCE.findAll());
  }
  /**
  * @MethodName: findAreaByPcode
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:33
  * @author jhf
  */
  public void findAreaByPcode() {
    renderJson(AreaService.INSTANCE.findByPcode(getPara("pcode", "0"),null, getPara("excludeCode", null)));
  }

  /**
  * @MethodName: getCodesByCodeTypeAndLeaf
  * @Description: 根据codetype和leaf获取数据字典
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:33
  * @author jhf
  */
  public void getCodesByCodeTypeAndLeaf(){
    renderJson(CodeService.INSTANCE.getCodesByCodeTypeAndLeaf(getPara("leaf"),getPara("codetype"),getPara("searchname"), getPara("pcode")));
  }

  /**
  * @MethodName: query
  * @Description: 自定义数据选项
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:33
  * @author jhf
  */
  public void query(){

    String params[] = getPara("param").split("\\.");
    String field = params[1].replace("A", "attrib_");

    StringBuilder sql = new StringBuilder();
    sql.append(String.format("select %s text,%s value from fn_form a, fn_form_data b " +
                    " where a.id = b.attrib_01 and a.name = ? "
            , field, field));

    List<Record> records = Db.find(sql.toString(), params[0]);

    renderJson(records);
  }
}
